﻿using Aspose.Cells;
using System.Data;

namespace tools.office.aspose
{
    /// <summary>
    /// Aspose Excel 工具类
    /// </summary>
    public static class AsposeExcelTool
    {
        /// <summary>
        /// 读取 Excel 文件转换为 DataTable
        /// </summary>
        /// <param name="filePath">Excel 文件完整路径</param>
        /// <returns>DataTable 实例</returns>
        public static DataTable GetDataTable(string? filePath)
        {
            if (!File.Exists(filePath))
            {
                throw new InvalidOperationException("Excel 文件不存在");
            }

            var wb = new Workbook(filePath);
            var ws = wb.Worksheets[0];

            int rowCount = ws.Cells.MaxDisplayRange.RowCount;
            int columnCount = ws.Cells.MaxDisplayRange.ColumnCount;
            return ws.Cells.ExportDataTable(0, 0, rowCount, columnCount, true);
        }
    }
}
